package eqsolver.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Observable;
import java.util.Observer;

import java.awt.*;
import javax.swing.*;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import eqsolver.ProxyEquation;

public class ParametersPanel extends JPanel implements Observer {

	private static final int PARAM_TOTAL = 5;
	private JTextField[] paramText = new JTextField[PARAM_TOTAL];
	private JLabel[] paramName = new JLabel[PARAM_TOTAL];

	private float[] param = new float[PARAM_TOTAL];
	private String[] paramNames = { "a", "b", "c", "d", "e" };
	private ProxyEquation equation;

	public ParametersPanel(ProxyEquation equation) {
		this.equation = equation;
		equation.addObserver(this);

		setBorder(BorderFactory.createEtchedBorder());
		setLayout(new GridLayout(PARAM_TOTAL, 2));

		// JLabel infoLabel = new JLabel("Parametri");
		// add(infoLabel);

		// ParamListener listener = new ParamListener();
		for (int i = 0; i < PARAM_TOTAL; i++) {
			paramName[i] = new JLabel(paramNames[i]);
			add(paramName[i]);

			this.paramText[i] = paramText[i] = new JTextField();
			add(this.paramText[i]);

			// paramText[i].addActionListener(new ParamListener(i));
			paramText[i].addKeyListener(new ParamListener(i));

		}

	}

	int var = 0;

	class ParamListener extends KeyAdapter implements ActionListener {// soluzione
																		// casalinga
																		// che
																		// comprende
		int index; // entrambi modi estendendo KeyAdapter
					// o implementando una cosa più generale
					// in questo caso uso KeyListener

		public ParamListener(int index) {
			this.index = index;
		}

		public void keyReleased(KeyEvent e) {
			super.keyReleased(e);
			String value = paramText[index].getText();

			try {
				Float floatvalue = new Float(value); // sarebbe meglio double,
														// però vabeh

				param[index] = floatvalue;

				// if (var<index+1) {
				// var = index;
				// }

				equation.setEquation(equation.getNumberParam(), param); // var+1
																		// perché
																		// parte
																		// da
																		// zero
				// System.out.println(index);
				System.out.println(equation.getParam()[index] + " index "
						+ equation.getNumberParam());
			} catch (NumberFormatException e1) {
				// e1.printStackTrace(); //in caso di spazio o carattere o nullo
				// quello che è
			}

			// Object src = e.getSource();

			// if (src == paramText[0]) {//si puo sostituire sta merda con un i
			// dentro ParamListener come da soluzione
			// System.out.println(paramText[0].getText());
			// } else if (src == paramText[1]){
			// System.out.println(paramText[1].getText());
			// } else if (src == paramText[2]){
			// System.out.println(paramText[2].getText());
			// } else if (src == paramText[3]){
			// System.out.println(paramText[1].getText());
			// } else if (src == paramText[4]){
			// paramText[4].getText();
			// } else {
			// //eccezione
			// }

			// System.out.println(paramText[0].getText());
		}

		@Override
		public void actionPerformed(ActionEvent e) { // inutilizzato

			// JTextField text = paramText[0];
			// JOptionPane.showMessageDialog(
			// null,text.getText());

			// Object src = e.getSource();
			//
			//
			//
			// if (src == paramText[0]) {
			// System.out.println(paramText[0].getText());
			// } else if (src == paramText[1]){
			// System.out.println(paramText[1].getText());
			// } else if (src == paramText[2]){
			// System.out.println(paramText[2].getText());
			// } else if (src == paramText[3]){
			// System.out.println(paramText[1].getText());
			// } else if (src == paramText[4]){
			// paramText[4].getText();
			// } else {
			// //eccezione
			// }

		}
	}

	public void update(Observable arg0, Object arg1) {

		int number = 2;
		number = equation.getNumberParam();

		System.out.println("dall'update " + equation.getNumberParam());
		for (int i = 0; i < number; i++) {
			paramText[i].setVisible(true);
		}

		for (int i = number; i < PARAM_TOTAL; i++) {
			paramText[i].setVisible(false);
		}

	}

}
